package com.ruoyi.client.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.client.domain.Address;
import com.ruoyi.client.mapper.AddressMapper;
import com.ruoyi.client.pojo.vo.AddressVo;
import com.ruoyi.client.service.IAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class AddressServiceImpl implements IAddressService {
    @Resource
    private AddressMapper addressMapper;

    @Override
    public List<AddressVo> list(Long userId) {
        return addressMapper.list(userId);
    }

    @Override
    public boolean save(Address address) {
        Long id = address.getId();
        if(ObjectUtil.isNull(id)){
            //新增
            return addressMapper.insert(address) > 0;
        }else{
            //更新
            return addressMapper.update(address) > 0;
        }
    }

    @Override
    public AddressVo selectById(Long id) {
        return addressMapper.selectById(id);
    }

    @Override
    public boolean delete(Long id) {
        return addressMapper.delete(id) > 0;
    }

    @Override
    public AddressVo getDefault(Long userId,Long id) {

        if(ObjectUtil.isNull(id)){
            //没有传递id就查询默认的收货地址
            return addressMapper.getDefault(userId);
        }else{
            //如果传递了id就查询id对应的收货地址
            return addressMapper.selectById(id);
        }
    }
}
